package com.dom.invoapp.modular.provider;

import com.baomidou.mybatisplus.plugins.Page;
import com.dom.api.purc.PurcOrderApi;
import com.dom.common.model.vo.apply.*;
import com.dom.common.utils.BeanUtil;
import com.dom.invoapp.dao.entity.TStorInfo;
import com.dom.invoapp.dao.entity.TpPurcCredDetailFlow;
import com.dom.invoapp.dao.entity.TpPurcOrderDetailFlow;
import com.dom.invoapp.dao.entity.TpPurcOrderFlow;
import com.dom.invoapp.dao.service.ITStorInfoService;
import com.dom.invoapp.modular.service.PurcOrderService;
import com.xiaoleilu.hutool.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 采购订单provider
 * @author xllei
 * @Date 2018-11-12
 */
@Slf4j
@RestController
public class PurcOrderProvider extends AbstractProvider implements PurcOrderApi {
    @Autowired
    private PurcOrderService purcOrderService;
    @Autowired
    private ITStorInfoService storInfoService;

    @Override
    public Page<OrderInfo> getPurcOrderList2(P039R p039R) {
        Page<TpPurcOrderFlow> page = purcOrderService.findByPage(p039R);

        Page<OrderInfo> result = new Page<>();
        BeanUtils.copyProperties(page, result);
        result.setRecords(null);

        List<OrderInfo> records = new ArrayList<>();
        page.getRecords().forEach(entity->{
            OrderInfo orderInfo = buildOrderPropertites(entity);
            boolean isOrder = (null != orderInfo && records.add(orderInfo));
        });

        result.setRecords(records);
        return result;
    }

    @Override
    public P039A getPurcOrderList(@RequestBody P039R p039R) {
        Page<OrderInfo> page = getPurcOrderList2(p039R);
        if(null == page){
            return null;
        }
        P039A response = new P039A();

        response.setOrderData(page.getRecords());
        response.setPn(page.getCurrent());
        response.setPs(page.getSize());
        response.setTotalNum((int)page.getTotal());
        response.setPages((int)page.getPages());
        return response;
    }

    @Override
    public String addPurcOrder(@RequestBody P030R request) {
        super.isValid(request);

        return purcOrderService.insertPurcOrder(request);
    }

    private OrderInfo buildOrderPropertites(TpPurcOrderFlow entity){
        if (null == entity) {
            return null;
        }
        OrderInfo orderInfo = new OrderInfo();
        BeanUtil.copyPropertiesIgnoreNull(entity, orderInfo);
        orderInfo.setOrderId(entity.getId());
        orderInfo.setExchDate(DateUtil.format(entity.getExchDate(), "yyyy-MM-dd"));
        orderInfo.setDeliDate(DateUtil.format(entity.getDeliDate(), "yyyy-MM-dd"));
        TStorInfo storInfo = null;
        if(!ObjectUtils.isEmpty(entity.getStorCode()) && 0 != entity.getStorCode() && null != ( storInfo = storInfoService.selectById(entity.getStorCode()))){
            orderInfo.setStorName(storInfo.getStorAbbr());
        }
        return orderInfo;
    }

    @Override
    public P049A getPurcOrderDetailList(@RequestBody P049R request) {
        List<TpPurcOrderDetailFlow> purcOrderDetailFlows = purcOrderService.findPurcOrderDetailList(request.getMemCode(), request.getOrderId());

        P049A response = new P049A();
        List<OrderDetailInfo> orderDetailInfos = new ArrayList<>();
        purcOrderDetailFlows.forEach(entity -> {
            OrderDetailInfo orderDetailInfo = new OrderDetailInfo();
            BeanUtil.copyPropertiesIgnoreNull(entity, orderDetailInfo);
            orderDetailInfo.setOrderDetailId(entity.getId());

            orderDetailInfos.add(orderDetailInfo);
        });

        response.setTotalNum(orderDetailInfos.size());
        response.setOrderDetailInfos(orderDetailInfos);

        return response;
    }

    @Override
    public P032A editPurcOrder(@RequestBody P032R request) {
        TpPurcOrderFlow orderFlow = purcOrderService.editPurcOrderInfo(request);

        List<TpPurcOrderDetailFlow> purcOrderDetailFlows = new ArrayList<>();
        request.getOrderDetailInfos().forEach(orderDetailInfo -> {
            TpPurcOrderDetailFlow purcOrderDetailFlow = purcOrderService.convertEntity(orderDetailInfo, orderFlow);
            purcOrderDetailFlows.add(purcOrderDetailFlow);
        });

        boolean result = purcOrderService.editPurcOrderDetail(request.getMemCode(), request.getOrderId(), purcOrderDetailFlows);

        P032A response = new P032A();
        response.setOrderId(orderFlow.getOrderId());
        return response;
    }
}
